/**
 * 
 */
package com.seguriapp.adapters;

import android.content.Context;
import android.database.Cursor;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckedTextView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

import com.seguriapp.dao.CrimeCategoryDao;
import com.seguriapp.dao.EntityDao;
import com.seguriapp.entities.Entity;

/**
 * @author pjohnson
 * 
 */
public class SpinnerAdapter extends SimpleCursorAdapter {

	public static final int POLICIES_ADAPTER = 0;
	public static final int CATEGORIES_ADAPTER = 1;
	private int mFlag;

	public SpinnerAdapter(Context context, int layout, Cursor c, String[] from,
			int[] to, final int flag) {
		super(context, layout, c, from, to);
		mFlag = flag;
	}

	/**
	 * Get the View of the new item to be displayed in the screen
	 */
	@Override
	public View getView(final int position, View convertView,
			final ViewGroup parent) {

		final TextView viewRelative = (TextView) super.getView(position,
				convertView, parent);
		final Cursor cursor = getCursor();
		cursor.moveToPosition(position);
		if (mFlag == POLICIES_ADAPTER) {
			viewRelative.setText(cursor.getString(cursor
					.getColumnIndexOrThrow(EntityDao.COL_TITLE)));
		} else {
			viewRelative.setText(cursor.getString(cursor
					.getColumnIndexOrThrow(CrimeCategoryDao.COL_NAME)));
		}

		return viewRelative;
	}

	@Override
	public View getDropDownView(int position, View convertView, ViewGroup parent) {
		final CheckedTextView viewRelative = (CheckedTextView) super
				.getDropDownView(position, convertView, parent);
		final Cursor cursor = getCursor();
		cursor.moveToPosition(position);

		if (mFlag == POLICIES_ADAPTER) {
			viewRelative.setText(cursor.getString(cursor
					.getColumnIndexOrThrow(EntityDao.COL_TITLE)));
		} else {
			viewRelative.setText(cursor.getString(cursor
					.getColumnIndexOrThrow(CrimeCategoryDao.COL_NAME)));
		}

		return viewRelative;
	}

	@Override
	public Object getItem(int position) {
		final Entity entity = new Entity();
		final Cursor cursor = getCursor();
		entity.setNombre(cursor.getString(cursor
				.getColumnIndexOrThrow(EntityDao.COL_TITLE)));
		entity.setId(cursor.getLong(cursor
				.getColumnIndexOrThrow(EntityDao.COL_ID)));
		entity.seteId(cursor.getLong(cursor
				.getColumnIndexOrThrow(EntityDao.COL_EID)));

		return entity;
	}

}
